var express = require('express');
var router = express.Router();
var mongoose = require('mongoose')
var Goods = require('../module/goods')
const Users = require('./../module/users')

mongoose.connect('mongodb://127.0.0.1:27017/shopmall')

mongoose.connection.on('connected',()=>{
console.log('succcess and ok')
})

mongoose.connection.on('error',()=>{
console.log('error and failure')
})

//商品列表操作
router.get('/',(req,res)=>{
    let page = parseInt(req.param('page'))
    let pageSize = parseInt(req.param('pageSize')) 
    let sort = parseInt(req.param('sort'))
    let skip = (page-1)*pageSize
    let priceLevel = parseInt(req.param('priceCheck'))
    var priceGT=''; var priceLT='';
    var obj = {}
    if(priceLevel!=-1){
    switch (priceLevel) {
        case 0: priceGT = 0; priceLT = 100; break;
        case 1: priceGT = 100; priceLT = 500; break;
        case 2: priceGT = 500; priceLT = 1000; break;
        case 3: priceGT = 1000; priceLT = 5000; break;
    }  
     obj = {
        salePrice:{
            $gt:priceGT,
            $lte:priceLT
        }
    }
    }

    let goodsModel = Goods.find(obj).skip(skip).limit(pageSize)
    goodsModel.sort({ 'salePrice':sort })
    goodsModel.exec((error,data)=>{
        if(error){
            res.json({
                status:1,
                msg:error.message
            })
         }else{
            res.json({
                status:0,
                msg:'',
                result:{
                    count:data.length,
                    list:data
                }
            })
        }
    })

})

//加入购物车操作
router.post('/addCar',(req,res)=>{
    var userId = req.cookies.userId
    var productId=req.body.productId
     Users.findOne({'userId':userId},(error1,userdata)=>{
         if(error1){
             res.json({
                 status: '1',
                 mes: error1.message,
             })
         }else{
             if(userdata){
                 var gooditem=''
                userdata.cartList.forEach(item=>{
                    if(item.productId==productId){
                        gooditem=item
                        gooditem.productNum++           
                    }
                })
                if(gooditem){
                    userdata.save((error3,doc)=>{
                        if(error3){
                        console.log('error3进入了');
                        }else{
                            res.json({
                                status:'0',
                                mes:'success and ok'
                            })
                        }
                    })
                }else{
                    Goods.findOne({ 'productId': productId }, (error2, productdata) => {
                        if (error2) {
                            res.json({
                                status: '1',
                                mes: error2.message,
                            })
                        } else {
                            if (productdata) {
                                userdata.cartList.push(productdata)
                                userdata.save((error3, doc) => {
                                    if (error3) {
                                        res.json({
                                            status: '1',
                                            mes: error3.message,
                                        })
                                    } else {
                                        res.json({
                                            status: '0',
                                            mes: 'success and ok',
                                            
                                        })
                                    }
                                })
                            }
                        }
                    })
                }



                //  Goods.findOne({'productId':productId},(error2,productdata)=>{
                //     if(error2){
                //         res.json({
                //             status: '1',
                //             mes: error2.message,
                //         })
                //     }else{
                //         if(productdata){  
                //             userdata.cartList.push(productdata)
                //             userdata.save((error3,doc)=>{
                //                 if(error3){
                //                     res.json({
                //                         status: '1',
                //                         mes: error3.message,
                //                     })
                //                 }else{
                //                     res.json({
                //                         status:'0',
                //                         mes:'success and ok',

                //                     })
                //                 }
                //             })
                //         }
                //     }
                //  })
             }
         }
     })
    })
    
 


module.exports = router



